{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 神经网络作业1\n",
    "\n",
    "利用现有的知识，将mnist提供的beginner模型优化至98%以上的准确率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\zhuhaier\\Anaconda3\\lib\\site-packages\\h5py\\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "参照beginner模板\n",
    "\"\"\"\n",
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "\n",
    "import argparse\n",
    "import sys\n",
    "\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "import tensorflow as tf\n",
    "\n",
    "FLAGS = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-2-d1d6deace759>:3: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "WARNING:tensorflow:From C:\\Users\\zhuhaier\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please write your own downloading logic.\n",
      "WARNING:tensorflow:From C:\\Users\\zhuhaier\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting input_data/train-images-idx3-ubyte.gz\n",
      "WARNING:tensorflow:From C:\\Users\\zhuhaier\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting input_data/train-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From C:\\Users\\zhuhaier\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.one_hot on tensors.\n",
      "Extracting input_data/t10k-images-idx3-ubyte.gz\n",
      "Extracting input_data/t10k-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From C:\\Users\\zhuhaier\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n"
     ]
    }
   ],
   "source": [
    "# Import data\n",
    "data_dir = 'input_data/'\n",
    "mnist = input_data.read_data_sets(data_dir, one_hot=True) #获得一个特有类型的数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 定义基本参数\n",
    "in_units=784\n",
    "out_units=10\n",
    "x = tf.placeholder(tf.float32, [None, in_units]) # 样本x：784维onehot特征的图片\n",
    "y_ = tf.placeholder(tf.float32, [None, out_units]) # y_：实际值矩阵\n",
    "\n",
    "# y = tf.matmul(x, W) + b    # 预测矩阵y，样本数*10列\n",
    "# W = tf.Variable(tf.zeros([784, 10])) # 初始化权重零矩阵W：784行权重特征，10列分类\n",
    "# b = tf.Variable(tf.zeros([10])) # 偏置b：10列预测偏置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 写一个建隐层函数，方便调参\n",
    "def hidden_layer(out1,out2,std):\n",
    "    #（0,1)正太分布初始化w权重，用0初始化b偏置。input-h1 h1-h2 h2-output共3套。\n",
    "    w1=tf.Variable(tf.contrib.layers.xavier_initializer()(([in_units,out1])))\n",
    "    b1=tf.Variable(tf.zeros(out1))\n",
    "    w2=tf.Variable(tf.contrib.layers.xavier_initializer()(([out1,out2])))\n",
    "    b2=tf.Variable(tf.zeros(out2))\n",
    "    w3=tf.Variable(tf.contrib.layers.xavier_initializer()(([out2,out_units])))\n",
    "    b3=tf.Variable(tf.zeros(out_units))\n",
    "    \n",
    "    h1=tf.nn.relu6(tf.matmul(x,w1)+b1)\n",
    "    h2=tf.nn.relu6(tf.matmul(h1,w2)+b2)\n",
    "    y_logits=tf.matmul(h2,w3)+b3\n",
    "    return y_logits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#先尝试用双隐层（形成任意二维空间形状的分类，在本例中肯定优于单隐层）与relu激活，每个隐层20个神经元\n",
    "out1=600\n",
    "out2=400\n",
    "std=0.05\n",
    "y_logits=hidden_layer(out1,out2,std)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-6-adfa42e706d8>:2: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "\n",
      "Future major versions of TensorFlow will allow gradients to flow\n",
      "into the labels input on backprop by default.\n",
      "\n",
      "See `tf.nn.softmax_cross_entropy_with_logits_v2`.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 计算交叉熵\n",
    "cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_logits))\n",
    "\n",
    "#加正则\n",
    "loss_reg=cross_entropy+0.1*(tf.nn.softmax(y_logits))\n",
    "\n",
    "# 定义train step\n",
    "train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)\n",
    "\n",
    "sess = tf.Session()\n",
    "\n",
    "init_op = tf.global_variables_initializer()\n",
    "sess.run(init_op)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 定义精度计算\n",
    "correct_prediction = tf.equal(tf.argmax(y_logits, 1), tf.argmax(y_, 1))\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.2638\n",
      "0.9442\n",
      "0.9709\n",
      "0.9708\n",
      "0.974\n",
      "0.9765\n",
      "0.977\n",
      "0.9802\n",
      "0.9772\n",
      "0.981\n",
      "0.9827\n",
      "0.9785\n",
      "0.9811\n",
      "0.9836\n"
     ]
    }
   ],
   "source": [
    "# Train\n",
    "for i in range(4200):\n",
    "    batch_xs, batch_ys = mnist.train.next_batch(100)\n",
    "    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})\n",
    "    if i%300==0:\n",
    "        print(sess.run(accuracy, feed_dict={x: mnist.test.images,y_: mnist.test.labels}))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 双隐层总结\n",
    "经过多次尝试，迭代次数越多，越精确，在学习率为0.4~0.6的情况下，4.2k×100个step即7个epoch后差不多精度能达到最大值。\n",
    "\n",
    "学习率如果低于0.4则进度偏慢，且对精度提升也没有积极影响。如果大于0.6则进度偏快，后期的提升能力不足。\n",
    "\n",
    "激活函数在本例以上指定参数情况下：\n",
    "- sigmoid由于梯度弥散的原因，只能达到93%\n",
    "- tanh还好，不过比relu还是差一些\n",
    "- relu6即prelu，结果和relu差不多，稍微平滑一些\n",
    "- softplus进度更缓慢，精度略逊\n",
    "- elu和swish比softplus略优，但还是不及relu\n",
    "\n",
    "以上分析结果可能受具体参数的影响。\n",
    "\n",
    "权重初始化中如果使用正太分布，则标准差std大于0.1时会出现神经元死亡现象，表现在精度的变化一般就是恒定在0.098。其中selu在0.05的标准差依然死亡。\n",
    "使用xavier初始化避免这个问题。结果相差不多。\n",
    "\n",
    "正则化不是很清楚如何添加，是否用手动累加权重进行计算，或是使用TensorFlow的支持包。暂时时间不足以研究。但考虑手写数字的形状的复杂性，正则项就算添加，其比重lambda也应该设置得小，所以我估计对最终影响不大。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
